package com.xjscrm.console.web.wwdevice;

import com.xjscrm.common.constant.ResponseMsg;
import com.xjscrm.common.exception.BusinessException;
import com.xjscrm.common.result.ResponseCode;
import com.xjscrm.console.param.BaseDto;
import com.xjscrm.console.param.user.ExternalUserIdRateSetDto;
import com.xjscrm.console.param.wwDevice.DeviceGroupDto;
import com.xjscrm.console.param.wwDevice.WwDeviceDto;
import com.xjscrm.console.param.wwDevice.WwDeviceQuery;
import com.xjscrm.console.service.customer.wwDevice.WwDeviceService;
import com.xjscrm.console.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.Map;

@Api(value = "机器人管理", tags = "机器人管理")
@RestController
@RequestMapping(value = "/wwdevice")
public class WwDeviceController {

    @Autowired
    private WwDeviceService wwDeviceService;

    @ApiOperation(value = "查询微信机器人", notes = "查询微信机器人",response = WwDeviceVo.class)
    @PostMapping(value ="/getWwDevice" )
    public Object getWwDevice(@RequestBody WwDeviceQuery wwDeviceQuery)throws Exception{
       return wwDeviceService.getWwDevice(wwDeviceQuery);
    }

    @ApiOperation(value = "查询微信机器人详情", notes = "查询微信机器人详情",response = WwDeviceVo.class)
    @PostMapping(value ="/getWwDevicedetail" )
    public Object getWwDevicedetail(@RequestBody WwDeviceDto wwDeviceDto)throws Exception{
        return wwDeviceService.getWwDevicedetail(wwDeviceDto);
    }

    @ApiOperation(value = "获取应用方案", notes = "获取应用方案",response = OperatePlanVo.class)
    @PostMapping(value ="/getOperatePlan" )
    public Object getOperatePlan(@RequestBody WwDeviceDto wwDeviceDto)throws Exception{
      return  wwDeviceService.getOperatePlan(wwDeviceDto);
    }

    @ApiOperation(value = "获取自动打标签方案", notes = "获取自动打标签方案", response = TaskTagAutoVo.class)
    @PostMapping(value = "/getTaskTagAuto")
    public Object getTaskTagAuto(@RequestBody WwDeviceDto wwDeviceDto) throws Exception {
        return wwDeviceService.getTaskTagAuto(wwDeviceDto);
    }

    @ApiOperation(value = "获取智能应答方案", notes = "获取智能应答方案", response = FaqReplyVo.class)
    @PostMapping(value = "/getFaqReply")
    public Object getFaqReply(@RequestBody WwDeviceDto wwDeviceDto) throws Exception {
        return wwDeviceService.getFaqReply(wwDeviceDto);
    }


    @ApiOperation(value = "获取拉群方案", notes = "获取拉群方案", response = GroupPullVo.class)
    @PostMapping(value = "/getGroupPull")
    public Object getGroupPull(@RequestBody WwDeviceDto wwDeviceDto) throws Exception {
        return wwDeviceService.getGroupPull(wwDeviceDto);
    }


    @ApiOperation(value = "获取欢迎语方案", notes = "获取欢迎语方案", response = GroupWelcomeVo.class)
    @PostMapping(value = "/getGroupWelcome")
    public Object getGroupWelcome(@RequestBody WwDeviceDto wwDeviceDto) throws Exception {
        return wwDeviceService.getGroupWelcome(wwDeviceDto);
    }

    @ApiOperation(value = "获取机器人分组", notes = "获取机器人分组",response = Map.class)
    @PostMapping(value ="/getDeviceGroup" )
    public Object getDeviceGroup(@RequestBody DeviceGroupDto deviceGroupDto)throws Exception{
        return  wwDeviceService.getDeviceGroup(deviceGroupDto);
    }

    @ApiOperation(value = "新增机器人分组", notes = "新增机器人分组",response = boolean.class)
    @PostMapping(value ="/addDeviceGroup" )
    public Object addDeviceGroup(@RequestBody DeviceGroupDto deviceGroupDto)throws Exception{
          wwDeviceService.addDeviceGroup(deviceGroupDto);
          return true;
    }

    @ApiOperation(value = "修改机器人分组", notes = "修改机器人分组",response = boolean.class)
    @PostMapping(value ="/updateDeviceGroup" )
    public Object updateDeviceGroup(@RequestBody DeviceGroupDto deviceGroupDto)throws Exception{
        wwDeviceService.updateDeviceGroup(deviceGroupDto);
        return true;
    }
    @ApiOperation(value = "删除机器人分组", notes = "删除机器人分组",response = boolean.class)
    @PostMapping(value ="/deleGroup" )
    public Object deleGroup(@RequestBody DeviceGroupDto deviceGroupDto)throws Exception{
        wwDeviceService.deleDeviceGroup(deviceGroupDto);
        return true;
    }

    @ApiOperation(value = "删除机器人分组关系", notes = "删除机器人分组",response = boolean.class)
    @PostMapping(value ="/deleDeviceGroup" )
    public Object deleDeviceGroup(@RequestBody DeviceGroupDto deviceGroupDto)throws Exception{
        wwDeviceService.deleDeviceGroupRelation(deviceGroupDto);
        return true;
    }

    @ApiOperation(value = "批量删除机器人分组关系", notes = "批量删除机器人分组",response = boolean.class)
    @PostMapping(value ="/batchDeleDeviceGroup" )
    public Object batchDeleDeviceGroup(@RequestBody DeviceGroupDto deviceGroupDto)throws Exception{
        if(deviceGroupDto.getDeviceUniqueIds()==null||deviceGroupDto.getDeviceUniqueIds().isEmpty()){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.PARAMETER_ERROR);
        }
        for (Long deviceUniqueId:deviceGroupDto.getDeviceUniqueIds()) {
            try {
                deviceGroupDto.setDeviceUniqueId(deviceUniqueId);
                wwDeviceService.deleDeviceGroupRelation(deviceGroupDto);
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        return true;
    }

    @ApiOperation(value = "批量删除机器人分组关系(选择全部)", notes = "批量删除机器人分组关系(选择全部)",response = boolean.class)
    @PostMapping(value ="/batchDeleDeviceGroupAll" )
    public Object batchDeleDeviceGroupAll(@RequestBody DeviceGroupDto deviceGroupDto)throws Exception{
        wwDeviceService.batchDeleDeviceGroupAll(deviceGroupDto);
        return true;
    }

    @ApiOperation(value = "移动机器人分组", notes = "移动机器人分组",response = boolean.class)
    @PostMapping(value ="/moveDeviceGroup" )
    public Object moveDeviceGroup(@RequestBody DeviceGroupDto deviceGroupDto)throws Exception{
        wwDeviceService.moveDeviceGroup(deviceGroupDto);
        return true;
    }

    @ApiOperation(value = "批量移动机器人分组", notes = "批量移动机器人分组",response = boolean.class)
    @PostMapping(value ="/batchMoveDeviceGroup" )
    public Object batchMoveDeviceGroup(@RequestBody DeviceGroupDto deviceGroupDto)throws Exception{
        if(deviceGroupDto.getDeviceUniqueIds()==null||deviceGroupDto.getDeviceUniqueIds().isEmpty()){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.PARAMETER_ERROR);
        }
        for (Long deviceUniqueId:deviceGroupDto.getDeviceUniqueIds()) {
            try {
                deviceGroupDto.setDeviceUniqueId(deviceUniqueId);
                wwDeviceService.moveDeviceGroup(deviceGroupDto);
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        return true;
    }
    @ApiOperation(value = "批量移动机器人分组(选择全部)", notes = "批量移动机器人分组(选择全部)",response = boolean.class)
    @PostMapping(value ="/batchMoveDeviceGroupAll" )
    public Object batchMoveDeviceGroupAll(@RequestBody DeviceGroupDto deviceGroupDto)throws Exception{
        wwDeviceService.batchMoveDeviceGroupAll(deviceGroupDto);
        return true;
    }

    @ApiOperation(value = "查询微信机器人在线数", notes = "查询微信机器人在线数",response = Integer.class)
    @PostMapping(value ="/onlineWwDeviceNum" )
    public Object onlineWwDeviceNum(@RequestBody BaseDto baseDto)throws Exception{
        return wwDeviceService.onlineWwDeviceNum(baseDto);
    }


    @ApiOperation(value = "获取机器人分组下拉", notes = "获取机器人分组下拉",response = Map.class)
    @PostMapping(value ="/getDeviceGroupDropdwon" )
    public Object getDeviceGroupDropdwon(@RequestBody BaseDto baseDto)throws Exception{
        return wwDeviceService.getDeviceGroupDropdwon(baseDto);
    }

    @ApiOperation(value = "获取机器人下拉", notes = "获取机器人下拉",response = Map.class)
    @PostMapping(value ="/getDeviceDropdwon" )
    public Object getDeviceDropDwon(@RequestBody BaseDto baseDto)throws Exception{
        return wwDeviceService.getDeviceDropDwon(baseDto);
    }

    @ApiOperation(value = "导出机器人数据", notes = "导出机器人数据")
    @PostMapping(value ="/exportDevice" )
    public void exportDevice(@RequestBody WwDeviceQuery wwDeviceQuery, HttpServletResponse response)throws Exception{
        wwDeviceService.exportDevice(wwDeviceQuery,response);
    }

    @ApiOperation(value = "修改获取外部账号id频率", notes = "修改获取外部账号id频率", response = boolean.class)
    @PostMapping(value ="/setExternalUseridRate" )
    public Object setExternalUseridRate(@RequestBody ExternalUserIdRateSetDto externalUserIdRateSetDto)throws Exception{
        wwDeviceService.setExternalUseridRate(externalUserIdRateSetDto);
        return true;
    }
}
